home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / FIND / FNMATCH.C < prev    next >
C/C++ Source or Header  |  1992-10-28  |  5KB  |  166 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Guido van Rossum.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37.  
  38. #if defined(LIBC_SCCS) && !defined(lint)
  39. static char sccsid[] = "@(#)fnmatch.c    5.4 (Berkeley) 2/23/91";
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. /*
  43.  * Function fnmatch() as proposed in Posix 1003.2 B.6 (rev. 9).
  44.  * Compares a filename or pathname to a pattern.
  45.  */
  46.  
  47. #ifdef DF_POSIX /* DF_MSS */
  48. #include <misc.h>
  49. #include <bsdlib.h>
  50. #endif
  51.  
  52. #include <unistd.h>
  53. #include <string.h>
  54.  
  55. #define    EOS    '\0'
  56.  
  57. static char *
  58. #if __STDC__
  59. rangematch (register const char *pattern, register char test)
  60. #else
  61. rangematch(pattern, test)
  62.     register const char *pattern, test;
  63. #endif
  64. {
  65.     register char c, c2;
  66.     int negate, ok;
  67.  
  68.     if ((negate = (*pattern == '!')) != 0)
  69.         ++pattern;
  70.  
  71.     /*
  72.      * TO DO: quoting
  73.      */
  74.  
  75.     for (ok = 0; (c = *pattern++) != ']';) {
  76.         if (c == EOS)
  77.             return(NULL);        /* illegal pattern */
  78.         if (*pattern == '-' && (c2 = pattern[1]) != EOS && c2 != ']') {
  79.             if (c <= test && test <= c2)
  80.                 ok = 1;
  81.             pattern += 2;
  82.         }
  83.         else if (c == test)
  84.             ok = 1;
  85.     }
  86.     return (char *)(ok == negate ? NULL : pattern);
  87. }
  88.  
  89. int
  90. #if __STDC__
  91. fnmatch (register const char *pattern, register const char *string, int flags)
  92. #else
  93. fnmatch(pattern, string, flags)
  94.     register const char *pattern;
  95.     register const char *string;
  96.     int flags;
  97. #endif
  98. {
  99.     register char c;
  100. #if WIN_NT
  101.     char test;
  102. #else
  103.     char test, *rangematch();
  104. #endif
  105.  
  106.     for (;;)
  107.         switch (c = *pattern++) {
  108.         case EOS:
  109.             return(*string == EOS);
  110.         case '?':
  111.             if ((test = *string++) == EOS ||
  112.                 test == '/' && flags & FNM_PATHNAME)
  113.                 return(0);
  114.             break;
  115.         case '*':
  116.             c = *pattern;
  117.             /* collapse multiple stars */
  118.             while (c == '*')
  119.                 c = *++pattern;
  120.  
  121.             /* optimize for pattern with * at end or before / */
  122.             if (c == EOS)
  123.                 if (flags & FNM_PATHNAME)
  124.                     return(!index(string, '/'));
  125.                 else
  126.                     return(1);
  127.             else if (c == '/' && flags & FNM_PATHNAME) {
  128.                 if ((string = index(string, '/')) == NULL)
  129.                     return(0);
  130.                 break;
  131.             }
  132.  
  133.             /* general case, use recursion */
  134.             while ((test = *string) != EOS) {
  135.                 if (fnmatch(pattern, string, flags))
  136.                     return(1);
  137.                 if (test == '/' && flags & FNM_PATHNAME)
  138.                     break;
  139.                 ++string;
  140.             }
  141.             return(0);
  142.         case '[':
  143.             if ((test = *string++) == EOS ||
  144.                 test == '/' && flags & FNM_PATHNAME)
  145.                 return(0);
  146.             if ((pattern = (const char *)rangematch(pattern, test)) == NULL)
  147.                 return(0);
  148.             break;
  149.         case '\\':
  150.             if (flags & FNM_QUOTE) {
  151.                 if ((c = *pattern++) == EOS) {
  152.                     c = '\\';
  153.                     --pattern;
  154.                 }
  155.                 if (c != *string++)
  156.                     return(0);
  157.                 break;
  158.             }
  159.             /* FALLTHROUGH */
  160.         default:
  161.             if (c != *string++)
  162.                 return(0);
  163.             break;
  164.         }
  165. }
  166.